// pages/report/report.js
const app = getApp();
const qiniu = require("../../utils/qiniu.min.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radioList:[{
      reportType: 0,
      text:"广告",
      isChoose:false
    }, {
      reportType: 1,
      text: "色情",
      isChoose: false
    }, {
      reportType: 2,
      text: "政治有害",
      isChoose: false
    }, {
      reportType: 3,
      text: "图片/文章侵权",
      isChoose: false
    }, {
      reportType: 4,
      text: "身份作假",
      isChoose: false
    }, {
      reportType: 5,
      text: "其他（辱骂、恶意灌水等）",
      isChoose: false
    }],
    lock:false,
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  setReason(e){
    var index = e.currentTarget.dataset.index;
    var text = e.currentTarget.dataset.text;
    var reportType = e.currentTarget.dataset.reporttype;
    for (var item of this.data.radioList){
      item.isChoose = false;
    }
    this.data.radioList[index].isChoose = true;
    this.toggle("radioList", this.data.radioList)
    this.toggle("text", text);
    this.toggle("reportType", reportType);
  },
  reasonInput(e){
    this.toggle("reason", e.detail.value);
  },
  //图片
  posterimg(e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    app.getQiNiuToken(function (res) {
      var countnum = 1
      var qiniuconfig = res;
      let timestamp = new Date().getTime();
      wx.chooseImage({
        count: countnum,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
          const filepathArray = res.tempFilePaths
          for (let i = 0; i < filepathArray.length; i++) {
            qiniu.upload(filepathArray[i], res => {
              that.toggle('posterUrl', res.imageURL)
            }, (error) => {
              // console.log('error' + error)
            },{
                uploadURL: 'https://upload.qiniup.com',
                region: 'ECN',
                domain: qiniuconfig.qiniuDomain,
                uptoken: qiniuconfig.upToken,
                key: timestamp + '.png',
            })
          }
        }
      })
    });
  },
  submit(){
    if(this.data.lock){
      return
    }
    var that = this;
    if(!this.data.text){
      wx.showToast({
        title: '请选择举报选项',
        icon:'none'
      })
      return
    }
    if (!this.data.reason) {
      wx.showToast({
        title: '请填写举报说明',
        icon: 'none'
      })
      return
    }
    app.req({
      url:"orgmini/reportOrg",
      method:"post",
      data: {
        "reportId": that.data.orgId,
        "reportReason": that.data.text,
        "reportType": that.data.reportType,
        "giveExample": that.data.reason,
        "giveExampleFile": that.data.posterUrl,
        "businessType": 6
      },
      success(res){
        if(res.code == 200){
          that.toggle("lock", true);
          wx.showToast({
            title: '举报成功，平台将会在24小时之内给出回复',
            icon:"none",
            duration: 3000
          })
          setTimeout(() =>{
            wx.navigateBack();
          },2000)
        }
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.toggle("orgId", options.orgId)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})